package cn.waynexuan.tool;

import java.util.*;

public class Month4Week {
	static private String stringFormat;
	static private String rangeFormat;

	private int year;
	private int month;
	private int startDay;
	private int endDay;
	private int week;

	Month4Week(int year, int month, int startDay, int endDay) {
		this.year = year;
		this.month = month;
		this.startDay = startDay;
		this.endDay = endDay;
	}

	public int getDays() {
		return endDay - startDay + 1;
	}

	static {
		int i = 0;
		rangeFormat = new StringBuilder()
				.append("%").append(++i).append("$").append("02d")  //startDay
				.append("-").append("%").append(++i).append("$").append("02d")  //endDay
				.toString();
		i = 0;
		stringFormat = new StringBuilder()
				.append("%").append(++i).append("$").append("4d")                //year
				.append(".").append("%").append(++i).append("$").append("02d")  //month
				.append("_").append("%").append(++i).append("$").append("1d")   //week
				.append(":").append("%").append(++i).append("$").append("02d")  //startDay
				.append("-").append("%").append(++i).append("$").append("02d")  //endDay
				.append("#").append("%").append(++i).append("$").append("02d")  //days
				.toString();
	}
	public String rangeString() {
		return String.format(rangeFormat, startDay, endDay);
	}

	@Override
	public String toString() {
		return String.format(stringFormat, year, month + 1, week, startDay, endDay, getDays());
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		Month4Week that = (Month4Week) o;
		return year == that.year &&
				month == that.month &&
				startDay == that.startDay &&
				endDay == that.endDay;
	}

	@Override
	public int hashCode() {
		return Objects.hash(year, month, startDay, endDay);
	}

	public int getYear() {
		return year;
	}

	public int getMonth() {
		return month;
	}

	public int getStartDay() {
		return startDay;
	}

	public int getEndDay() {
		return endDay;
	}

	public int getWeek() {
		return week;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public void setStartDay(int startDay) {
		this.startDay = startDay;
	}

	public void setEndDay(int endDay) {
		this.endDay = endDay;
	}

	public void setWeek(int week) {
		this.week = week;
	}
}
